/*
 * Tencent is pleased to support the open source community by making QMUI_Android available.
 *
 * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the MIT License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pj.common.decorator

import android.content.Context
import kotlin.jvm.JvmOverloads
import com.pj.common.R
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.skin.IQMUISkinHandlerDecoration
import androidx.core.view.ViewCompat
import com.qmuiteam.qmui.skin.QMUISkinManager
import android.content.res.Resources.Theme
import android.graphics.Canvas
import android.graphics.Paint
import com.qmuiteam.qmui.util.QMUIResHelper

/**
 * @author cginechen
 * @date 2016-10-21
 */
class GridDividerItemDecoration @JvmOverloads constructor(
    context: Context?,
    private val mSpanCount: Int,
    private val mDividerAttr: Int = R.attr.qmui_skin_support_color_separator,
    dividerWidth: Float = 1f
) : RecyclerView.ItemDecoration(), IQMUISkinHandlerDecoration {
    private val mDividerPaint = Paint()
    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val position = parent.getChildLayoutPosition(child)
            val column = (position + 1) % mSpanCount
            val params = child
                .layoutParams as RecyclerView.LayoutParams
            val childBottom = child.bottom + params.bottomMargin
//                    Math.round(ViewCompat.getTranslationY(child))
            val childRight = child.right + params.rightMargin
                    //Math.round(ViewCompat.getTranslationX(child))
            if (childBottom < parent.height) {
                c.drawLine(
                    child.left.toFloat(),
                    childBottom.toFloat(),
                    childRight.toFloat(),
                    childBottom.toFloat(),
                    mDividerPaint
                )
            }
            if (column < mSpanCount) {
                c.drawLine(
                    childRight.toFloat(),
                    child.top.toFloat(),
                    childRight.toFloat(),
                    childBottom.toFloat(),
                    mDividerPaint
                )
            }
        }
    }

    override fun handle(
        recyclerView: RecyclerView,
        manager: QMUISkinManager,
        skinIndex: Int,
        theme: Theme
    ) {
        mDividerPaint.color = QMUIResHelper.getAttrColor(theme, mDividerAttr)
        recyclerView.invalidate()
    }

    init {
        mDividerPaint.strokeWidth = dividerWidth
        mDividerPaint.style = Paint.Style.STROKE
        mDividerPaint.color = QMUIResHelper.getAttrColor(context, mDividerAttr)
    }
}